//
//  NetWorkLoginAndRegister.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/21.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "NetWorkLoginAndRegister.h"

@implementation NetWorkLoginAndRegister

//获取验证码
+(void)getSmsCode:(NSString *)code withSuccessBlock:(void(^)(id responseObj))successBlock{
    [NetWorkTool requestPostWithUrl:getCommonCode WithParams:@{@"telphone":code} withSuccessBlock:^(id  _Nonnull responseObj) {
        [[XNProgressHUD shared]showSuccessWithTitle:@"验证码发送成功，请注意查收"];
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared]showErrorWithTitle:errorStr];
    }];
}

//注册
+(void)registerWithParams:(NSDictionary *)params withSuccessBlock:(void(^)(id responseObj))successBlock{
    [NetWorkTool requestPostWithUrl:userRegister WithParams:params withSuccessBlock:^(id  _Nonnull responseObj) {
        [[XNProgressHUD shared] showSuccessWithTitle:@"注册成功"];
        NSDictionary *data = responseObj[@"data"];
        [NetWorkLoginAndRegister saveUserInfo:data];
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared]showErrorWithTitle:errorStr];
    }];
}

//登录
+(void)loginWithParams:(NSDictionary *)params withSuccessBlock:(void(^)(id responseObj))successBlock{
    [NetWorkTool requestPostWithUrl:userLogin WithParams:params withSuccessBlock:^(id  _Nonnull responseObj) {
        [[XNProgressHUD shared] showSuccessWithTitle:@"登录成功"];
        NSDictionary *data = responseObj[@"data"];
        [NetWorkLoginAndRegister saveUserInfo:data];
        successBlock(responseObj);
    } withFailedBlock:^(NSString * _Nonnull errorStr) {
        [[XNProgressHUD shared]showErrorWithTitle:errorStr];
    }];
}

//异步 存储用户信息
+(void)saveUserInfo:(NSDictionary *)userInfo{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *token = userInfo[@"token"];
        NSInteger userId = [userInfo[@"userId"] integerValue];
        [UserInfomationManager shareManager].token = token;
        [UserInfomationManager shareManager].userId = userId;
        [UserInfomationManager shareManager].loginStatus = YES;
        
        [NetWorkingPerson getUserHomeWithSuccessBlock:^(id  _Nonnull responseObj) {
            NSDictionary *data = responseObj[@"data"];
            NSDictionary *userDict = data[@"user"];
            [UserInfomationManager updateUserInfo:userDict];
        }];
    });
}




@end
